<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/User.Master" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<script runat="server">

    
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Detail")
        {
            string id = (DataList1.FindControl("IDLabel") as Label).Text;
            Response.Redirect("/Home/ProgramDetail/?id=" + id);
        }
    }
     

    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = e.Item.ItemIndex;
        DataList1.DataBind();
    }

    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = -1;
        DataList1.DataBind();   
    }

    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        SqlDataSource1.UpdateParameters[0].DefaultValue = (e.Item.FindControl("TextBox1") as TextBox).Text;
        SqlDataSource1.UpdateParameters[1].DefaultValue = (e.Item.FindControl("dl1") as DropDownList).Text;
        SqlDataSource1.UpdateParameters[2].DefaultValue = (e.Item.FindControl("TextBox3") as TextBox).Text;
        SqlDataSource1.UpdateParameters[3].DefaultValue = (e.Item.FindControl("TextBox4") as TextBox).Text;
        SqlDataSource1.UpdateParameters[4].DefaultValue = (e.Item.FindControl("TextBox5") as TextBox).Text;
        SqlDataSource1.UpdateParameters[5].DefaultValue = (e.Item.FindControl("IDLabel") as Label).Text;
        SqlDataSource1.Update();
        
        
        DataList1.EditItemIndex = -1;
        DataList1.DataBind();
    }

    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        SqlDataSource1.DeleteParameters[0].DefaultValue = (e.Item.FindControl("IDLabel") as Label).Text;
        SqlDataSource1.Delete();
    }

    protected void NewProgram_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        NewProgram.Visible = false;
    }



    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        NewProgram.Visible = true;
    }


    protected void btnInsert_Click(object sender, EventArgs e)
    {
        SqlDataSource1.InsertParameters[0].DefaultValue = (Panel1.FindControl("TextBox1") as TextBox).Text;
        SqlDataSource1.InsertParameters[1].DefaultValue = (Panel1.FindControl("dl1") as DropDownList).Text;
        SqlDataSource1.InsertParameters[2].DefaultValue = (Panel1.FindControl("TextBox3") as TextBox).Text;
        SqlDataSource1.InsertParameters[3].DefaultValue = (Panel1.FindControl("TextBox4") as TextBox).Text;
        SqlDataSource1.InsertParameters[4].DefaultValue = (Panel1.FindControl("TextBox5") as TextBox).Text;
        SqlDataSource1.InsertParameters[5].DefaultValue = "1";

        SqlDataSource1.Insert();
    }

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {


    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	
	
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h1>MANAGE PROGRAM </h1>
    <asp:DataList ID="DataList1" runat="server" DataKeyField="ID" 
        DataSourceID="SqlDataSource1" oneditcommand="DataList1_EditCommand" 
        Width="916px" oncancelcommand="DataList1_CancelCommand" 
        onupdatecommand="DataList1_UpdateCommand" 
        ondeletecommand="DataList1_DeleteCommand" 
        onitemcommand="DataList1_ItemCommand">
        <AlternatingItemTemplate>
            <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' 
                Visible="False" />
        </AlternatingItemTemplate>
        <EditItemTemplate>
            Name:
            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
            <br />
            Dean:
            <asp:DropDownList ID="dl1" runat="server" 
                DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="ID" 
                SelectedValue='<%# Eval("Dean") %>'>
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:CDIO1ConnectionString %>" 
                SelectCommand="SELECT [ID], [Name] FROM [Professor]"></asp:SqlDataSource>
            <br />
            Description:
            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("Description") %>'></asp:TextBox>
            <br />
            Objective:
            <asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("Objective") %>'></asp:TextBox>
            <br />
            ProgramLO:
            <asp:TextBox ID="TextBox5" runat="server" Text='<%# Eval("ProgramLO") %>'></asp:TextBox>
            <br />
            <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' 
                Visible="False" />
            <br />
            <asp:LinkButton ID="LinkButton3" runat="server" CommandName="Update">Update</asp:LinkButton>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <asp:LinkButton ID="LinkButton4" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
        </EditItemTemplate>
        <SelectedItemTemplate>
            <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' 
                Visible="False" />
        </SelectedItemTemplate>
        <ItemTemplate>
            <table style="width: 124%;">
                <tr>
                    <td style="width: 401px" bgcolor="#33CCFF">
                        &nbsp;<asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' 
                            Visible="False" />
                        <br />
                        <b>Name</b>:
                        <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                        <br />
                        <b>Dean</b>:
                        <asp:Label ID="DeanLabel" runat="server" Text='<%# Eval("Expr1") %>' />
                        <br />
                        <b>Objective</b>:
                        <asp:Label ID="ObjectiveLabel" runat="server" Text='<%# Eval("Objective") %>' />
                        <br />
                        <b>ProgramLO</b>:
                        <asp:Label ID="ProgramLOLabel" runat="server" Text='<%# Eval("ProgramLO") %>' />
                        <br />
                        &nbsp;<asp:Label ID="StatusLabel" runat="server" Text='<%# Eval("Status") %>' 
                            Visible="False" />
                        <br />
                        <br />
                    </td>
                    <td bgcolor="#66FFFF">
                        <b>Description</b>:
                        <asp:Label ID="DescriptionLabel" runat="server" 
                            Text='<%# Eval("Description") %>' />
                    </td>
                </tr>
            </table>
            &nbsp;&nbsp; &nbsp;&nbsp;
            <asp:LinkButton ID="LinkButton5" runat="server" CommandName="Detail" 
                PostBackUrl='<%# Bind("ID", "/Home/ProgramDetail/?id={0}") %>'>Detail</asp:LinkButton>
            <br />
        </ItemTemplate>
    </asp:DataList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:CDIO1ConnectionString %>" 
        DeleteCommand="UPDATE [Program] SET [Status] = 0 WHERE [ID] = @ID" 
        InsertCommand="INSERT INTO [Program] ([Name], [Dean], [Description], [Objective], [ProgramLO], [Status]) VALUES (@Name, @Dean, @Description, @Objective, @ProgramLO, @Status)" 
        SelectCommand="SELECT Program.ID, Program.Name, Program.Dean, Program.Description, Program.Objective, Program.ProgramLO, Program.Status, Professor.Name AS Expr1 FROM Program INNER JOIN Professor ON Program.Dean = Professor.ID WHERE (Program.Status = @Status)" 
        
        UpdateCommand="UPDATE [Program] SET [Name] = @Name, [Dean] = @Dean, [Description] = @Description, [Objective] = @Objective, [ProgramLO] = @ProgramLO WHERE [ID] = @ID">
        <SelectParameters>
            <asp:Parameter DefaultValue="1" Name="Status" Type="Int32" />
        </SelectParameters>
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Dean" Type="Int32" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="Objective" Type="String" />
            <asp:Parameter Name="ProgramLO" Type="String" />
            <asp:Parameter Name="ID" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Dean" Type="Int32" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="Objective" Type="String" />
            <asp:Parameter Name="ProgramLO" Type="String" />
            <asp:Parameter Name="Status" Type="Int32" />
        </InsertParameters>
    </asp:SqlDataSource>

    
    <asp:Button ID="NewProgram" runat="server" onclick="NewProgram_Click" 
        Text="New" Visible="False" />
    <br />
    <asp:Panel ID="Panel1" runat="server" Visible="False">
        Name:
        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
        <br />
        Dean:
        <asp:DropDownList ID="dl1" runat="server" DataSourceID="Data" 
            DataTextField="Name" DataValueField="ID">
        </asp:DropDownList>
        <asp:SqlDataSource ID="Data" runat="server" 
            ConnectionString="<%$ ConnectionStrings:CDIO1ConnectionString %>" 
            SelectCommand="SELECT [ID], [Name] FROM [Professor]"></asp:SqlDataSource>
        <br />
        Description:
        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("Description") %>'></asp:TextBox>
        <br />
        Objective:
        <asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("Objective") %>'></asp:TextBox>
        <br />
        ProgramLO:
        <asp:TextBox ID="TextBox5" runat="server" Text='<%# Eval("ProgramLO") %>'></asp:TextBox>
        <br />
        <br />
        <br />
        <asp:Button ID="btnInsert" runat="server" Text="Insert" 
            onclick="btnInsert_Click" />
        &nbsp;&nbsp;
        <asp:Button ID="btnCancel" runat="server" Text="Cancel" 
            onclick="btnCancel_Click" />
    </asp:Panel>
                    
    
</asp:Content>
